package com.xuexiang.yoyo.sql.mapper;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import androidx.annotation.NonNull;


import com.fcat.base.common.util.DateUtil;
import com.xuexiang.yoyo.constant.HesAppStatic;
import com.xuexiang.yoyo.entity.DataItemParam;

import java.util.ArrayList;
import java.util.List;

public class DataItemParamMapper {

    /*
    private Long id;            1
    private String group_name;2
    private String sub_group_name;3
    private String ln;4
    private String attribute_index;5
    private String data_item_name;6
    private String param_name;7
    private String type;8
    private Boolean displaylist;9
    private Boolean billing;10
    private Boolean demand;11
    private Boolean loadprofile;12
    private Boolean collection_programme;13
    private Boolean local_reading;14
    private Date create_time;15
    private Date update_time;16
    private String short_name;17
    private String gxdlms_class_name;18
    private String cosem_class_id;19
    private String cosem_class_version;20
     */
    public static final String TABLE = "data_item_param";
    public static final String CREATE_TABLE_SQL = "CREATE TABLE " + TABLE + "(id INTEGER PRIMARY KEY AUTOINCREMENT," +
            "group_name TEXT ," +
            "sub_group_name TEXT," +
            "ln TEXT," +
            "attribute_index TEXT," +
            "data_item_name TEXT," +
            "param_name TEXT," +
            "type TEXT," +
            "displaylist INTEGER," +
            "billing INTEGER," +
            "demand INTEGER," +
            "loadprofile INTEGER," +
            "collection_programme INTEGER," +
            "local_reading INTEGER," +
            "create_time TEXT," +
            "update_time TEXT," +
            "short_name TEXT," +
            "gxdlms_class_name TEXT," +
            "cosem_class_id TEXT," +
            "cosem_class_version TEXT" +
            ");";


    public static void insert(DataItemParam item) {
        ContentValues contentValues = new ContentValues();
        setContentValues(contentValues, item);
        HesAppStatic.getmDataBaseOpenHelper().insert(TABLE, contentValues);
    }

    public static void clear() {
        HesAppStatic.getmDataBaseOpenHelper().delete(TABLE, null, null);
    }

    public static List<DataItemParam> queryAll() {
        Cursor cursor = HesAppStatic.getmDataBaseOpenHelper().query(TABLE, "");
        List<DataItemParam> resultList = getDataItemParams(cursor);
        cursor.close();
        return resultList;
    }

    private static void setContentValues(ContentValues contentValues, DataItemParam item) {
        contentValues.put("group_name", item.getGroupName());
        contentValues.put("sub_group_name", item.getSubGroupName());
        contentValues.put("ln", item.getLn());
        contentValues.put("attribute_index", item.getAttributeIndex());
        contentValues.put("data_item_name", item.getDataItemName());
        contentValues.put("param_name", item.getParamName());
        contentValues.put("displaylist", item.getDisplaylist());
        contentValues.put("billing", item.getBilling());
        contentValues.put("demand", item.getDemand());
        contentValues.put("loadprofile", item.getLoadprofile());
        contentValues.put("collection_programme", item.getCollectionProgramme());
        contentValues.put("local_reading", item.getLocalReading());
        contentValues.put("create_time", DateUtil.formatDate(item.getCreateTime(), DateUtil.TIME_PATTERN_DISPLAY));
        contentValues.put("update_time", DateUtil.formatDate(item.getUpdateTime(), DateUtil.TIME_PATTERN_DISPLAY));
        contentValues.put("short_name", item.getShortName());
        contentValues.put("gxdlms_class_name", item.getGxdlmsClassName());
        contentValues.put("cosem_class_id", item.getCosemClassId());
        contentValues.put("cosem_class_version", item.getCosemClassVersion());
    }


    public static void batchInsert(List<DataItemParam> list) {
        //插入之前先清空数据
        clear();
        SQLiteDatabase db = HesAppStatic.getmDataBaseOpenHelper().getWritableDatabase();
        db.beginTransaction();
        try {
            ContentValues contentValues = new ContentValues();
            for (DataItemParam item : list) {
                setContentValues(contentValues, item);
                db.insert(TABLE, null, contentValues);
                contentValues.clear();
            }
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();

        }
    }


    @NonNull
    private static List<DataItemParam> getDataItemParams(Cursor cursor) {
        List<DataItemParam> resultList = new ArrayList<>();
        int i=0;
        while (cursor.moveToNext()) {
            i++;
            DataItemParam item = new DataItemParam();
            item.setId(cursor.getLong(0));
            item.setGroupName(cursor.getString(1));
            item.setSubGroupName(cursor.getString(2));
            item.setLn(cursor.getString(3));
            item.setAttributeIndex(cursor.getString(4));
            item.setDataItemName(cursor.getString(5));
            item.setParamName(cursor.getString(6));
            item.setType(cursor.getString(7));
            item.setDisplaylist(cursor.getInt(8) == 0 ? false : true);
            item.setBilling(cursor.getInt(9) == 0 ? false : true);
            item.setDemand(cursor.getInt(10) == 0 ? false : true);
            item.setLoadprofile(cursor.getInt(11) == 0 ? false : true);
            item.setCollectionProgramme(cursor.getInt(12) == 0 ? false : true);
            item.setLocalReading(cursor.getInt(13) == 0 ? false : true);
            item.setCreateTime(DateUtil.toDate(cursor.getString(14), DateUtil.TIME_PATTERN_DISPLAY));
            item.setUpdateTime(DateUtil.toDate(cursor.getString(15), DateUtil.TIME_PATTERN_DISPLAY));
            item.setShortName(cursor.getString(16));
            item.setGxdlmsClassName(cursor.getString(17));
            item.setCosemClassId(cursor.getString(18));
            item.setCosemClassVersion(cursor.getString(19));
            resultList.add(item);
        }
        return resultList;
    }


}
